//
//  sdfViewController.m
//  JYDanmu
//
//  Created by jianyong.wang on 2017/12/20.
//  Copyright © 2017年 jianyong.wang. All rights reserved.
//

#import "sdfViewController.h"
#import "JYDMView.h"
#define KZJZIJKSCREENWIDTH          [UIScreen mainScreen].bounds.size.width
#define KZJZIJKSCREENHEIGHT         [UIScreen mainScreen].bounds.size.height
@interface sdfViewController ()
@property (nonatomic, strong) JYDMView *danmu;
@property (nonatomic, strong) NSTimer  *timer;

@end

@implementation sdfViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.danmu = [[JYDMView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.width) danMuHeight:50.f rollTime:5.f];
        [self.view addSubview:self.danmu];
        NSArray *array = @[@"开始", @"结束", @"全屏切换"];
        CGFloat w = self.view.frame.size.width/3;
        for (int i = 0; i < 3; i ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = 100 + i;
            btn.frame = CGRectMake(w * i, self.danmu.frame.size.height + 40, w, 40);
            [btn setTitle:array[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(onBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btn];
        }
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (void)onBtnClicked:(UIButton *)button
    {
        if (button.tag == 100) {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(onTimerClicked:) userInfo:nil repeats:YES];
//                  [self.danmu sendDMWithContent:@"😆dsfsdfsdfdsf"];
            
        } else if (button.tag == 101) {
            [self.timer invalidate];
        } else {
            
            if (button.selected) {
                
                button.selected = NO;
                [self.danmu resetFrame:CGRectMake(0, 20, KZJZIJKSCREENWIDTH,  KZJZIJKSCREENWIDTH* 9/16)];
                
                [UIView animateWithDuration:0.4f animations:^{
                    
                    // 逆时针旋转90度
                    [self.danmu setTransform:CGAffineTransformMakeRotation(90 * M_PI)];
                    self.danmu.frame = CGRectMake(0, 20, KZJZIJKSCREENWIDTH, KZJZIJKSCREENWIDTH * 9/16);
                    
                    [self.danmu layoutIfNeeded];
                }];
                
            } else {
                
                button.selected = YES;
                
                // 置于父视图顶层
                //[[self superview] bringSubviewToFront:self];
                
                [self.danmu resetFrame:CGRectMake(0, 0, KZJZIJKSCREENHEIGHT, KZJZIJKSCREENWIDTH)];
                [UIView animateWithDuration:0.4f animations:^{
                    
                    // 顺时针旋转90度
                    [self.danmu setTransform:CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI_2), (KZJZIJKSCREENWIDTH - KZJZIJKSCREENWIDTH * 9/16) / 2 - 20, (KZJZIJKSCREENWIDTH - KZJZIJKSCREENWIDTH * 9/16) / 2)];
                    
                    self.danmu.frame = CGRectMake(0, 0, KZJZIJKSCREENWIDTH, KZJZIJKSCREENHEIGHT);
                    
                    [self.danmu layoutIfNeeded];
                    
                }];
            }
        }
    }
    - (void)onTimerClicked:(NSTimer *)timer
    {
        [self.danmu sendDMWithContent:@"亮dsffsd亮dsffsdfd亮dsffsdfd亮dsffsdfd亮dsffsdfd亮dsffsdfd亮dsffsdfd亮dsffsdfd亮dsffsdfdfdsf"];
    }
 -(void)dealloc
{
    if (self.timer.valid) {
        [self.timer invalidate];
        self.timer = nil;
    }
    
}
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

@end
